<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑产品信息/办理要求</title>
    <c:import url="../../header.jsp"/>
</head>
<body>

<div class="content-warp">
    <div class="bg-warp">
        <table class="dggui-table cell-border table-form nodeTable">
            <thead>
            <th>序号</th>
            <th>字段名</th>
            <th>字段值</th>
            <th colspan="2">操作</th>
            </thead>
            <tbody id="tb">
            <c:if test="${empty evaluatingChannelFields}">
                <tr>
                    <td>1</td>
                    <td><input type="text" class="dggui-input" maxlength="50" /></td>
                    <td>
                        <input type="text" class="dggui-input" />
                    </td>
                    <td>
                    </td>
                    <td>
                        <a href="javascript:void(0)" class="dggui-icon-btn" onclick="addRow(this)" ><i class="iconfont icon-tianjia"></i></a>
                    </td>
                </tr>
            </c:if>
            <c:if test="${!empty evaluatingChannelFields}">
                <c:forEach items="${evaluatingChannelFields}" var="key" varStatus="index">
                    <tr>
                    <td>${index.index+1}</td>
                    <td><input type="text" class="dggui-input" maxlength="50" value="${key.fieldName}" /></td>
                    <td>
                        <input type="text" class="dggui-input" value="${key.fieldValue}" />
                    </td>
                    <td>
                        <c:if test="${index.index!=0}">
                            <a href="javascript:void(0)" class="iboss-link" onclick="moveFun(this,1)">上移</a>
                            <a href="javascript:void(0)" class="iboss-link" onclick="moveFun(this,2)">下移</a>
                        </c:if>

                    </td>
                    <td>
                        <c:if test="${index.index!=0}">
                            <a href="javascript:void(0)" class="dggui-icon-btn" onclick="removeRow(this)"><i class="iconfont icon-shanchu"></i></a>
                        </c:if>
                        <a href="javascript:void(0)" class="dggui-icon-btn" onclick="addRow(this)" ><i class="iconfont icon-tianjia"></i></a>
                    </td>
                </tr>
                </c:forEach>
            </c:if>
            </tbody>
        </table>
        <div class="txt-center">
            <button class="dggui-btn" onclick="saveNodeFlow();">保存</button>
        </div>
    </div>
</div>
<c:import url="/WEB-INF/jsp/footer.jsp"/>
<script>
    dggui.use(['jquery','dialog'],function () {
    })
    //新增行
    function addRow(obj) {
        var trHtm='<tr>' +
            '            <td></td>\n' +
            '            <td><input type="text" class="dggui-input" maxlength="50" /></td>\n' +
            '            <td>' +
            '               <input type="text" class="dggui-input" />' +
            '           </td>' +
            '            <td>\n' +
            '                <a href="javascript:void(0)" class="iboss-link" onclick="moveFun(this,1)">上移</a>\n' +
            '                <a href="javascript:void(0)" class="iboss-link" onclick="moveFun(this,2)">下移</a>\n' +
            '            </td>\n' +
            '            <td>\n' +
            '                <a href="javascript:void(0)" class="dggui-icon-btn" onclick="removeRow(this)"><i class="iconfont icon-shanchu"></i></a>\n' +
            '                 <a href="javascript:void(0)" class="dggui-icon-btn" onclick="addRow(this)" ><i class="iconfont icon-tianjia"></i></a>' +
            '            </td>\n' +
            '        </tr>'
        $(obj).parents('tr').after(trHtm);
        tableEach();
    }
    //删除行
    function removeRow(obj) {
        $(obj).parents('tr').remove();
        tableEach()
    }
    //上下移动行
    function moveFun(obj,move) {
        var num = $(obj).parents('tr').find('td').first().text();
        var totalNum=$('.nodeTable').find('tbody').find('tr').length;
        if((num == 1 || num == 2) && move == 1){
            return;
        }
        if(num == totalNum  && move == 2){
            return;
        }
        var trHtm=$(obj).parents('tr').html();
        trHtm='<tr>'+trHtm+'</tr>';
        /*获取移动行内输入框的val*/
        var tdLen=$(obj).parents('tr').find('td').length-2;
        var tdData={};
        $(obj).parents('tr').find('td').each(function (i) {
            if(i<tdLen) {
                tdData[i]=$(this).children().val();
            }
        });
        if(move==1) {
            //上移
            $(obj).parents('tr').prev().before(trHtm);
            $(obj).parents('tr').prev().prev().find('td').each(function (j) {
                $(this).children().val(tdData[j])
            })
        }
        else {
            //下移
            $(obj).parents('tr').next().after(trHtm);
            $(obj).parents('tr').next().next().find('td').each(function (j) {
                $(this).children().val(tdData[j])
            })
        }
        $(obj).parents('tr').remove();
        tableEach();
    }
    function tableEach() {
        var trLen=$('.nodeTable').find('tbody').find('tr').length;
        $('.nodeTable').find('tbody').find('tr').each(function (i) {
            $(this).children().eq(0).text(i+1)
            if(i==0) {
            }else if(i==trLen-1) {
            }else {
                $(this).children().eq(3).empty().append('<a href="javascript:void(0)" class="iboss-link" onclick="moveFun(this,1)">上移</a>' +
                    '<a href="javascript:void(0)" class="iboss-link" onclick="moveFun(this,2)">下移</a>');
            }
        })
    }

    function saveNodeFlow(){
        var channelId = '${channelId}';//渠道ID
        var channelCode = '${channelCode}';//渠道code
        var fieldType = '${fieldType}';//1、产品信息 ；2、办理要求
        var trList = $("#tb").children("tr");
        var tableParms = [];
        for (var i = 0; i < trList.length; i++) {
            var tdArr = trList.eq(i).find("td");
            var fieldSort = $.trim(tdArr.eq(0).text());//序号
            var fieldName = tdArr.eq(1).find('input').val();//名称
            var fieldValue = tdArr.eq(2).find('input').val();//值
            if (!fieldName) {
                dggui.dialog.errors('请确认字段名是否完整！');
                return;
            }
            if (!fieldValue) {
                dggui.dialog.errors('请确认字段值是否完整！');
                return;
            }
            var parm = {};
            parm.channelId = channelId;
            parm.channelCode = channelCode;
            parm.fieldType = fieldType;
            parm.fieldSort = fieldSort;
            parm.fieldName = fieldName;
            parm.fieldValue = fieldValue;
            tableParms.push(parm);
        }
        var params = {};
        params.evaluatingChannelFields = tableParms;
        params.channelId = channelId;
        params.fieldType = fieldType;
        dggui.dialog.loading();
        $.ajax({
            type: 'POST',
            async: true,
            url: sysInfo.basePath + '/rzsc/evaluating/save_channel_field.do',
            data: JSON.stringify(params),
            dataType: 'json',
            contentType:"application/json",
            success: function (data) {
                dggui.dialog.loading('close');
                if (data.code == 0) {
                    dggui.dialog.succeed('保存成功！');
                    setTimeout("location.reload()",1300);
                } else {
                    dggui.dialog.errors(data.msg);
                }
            },
            error: function () {
                dggui.dialog.loading('close');
                dggui.dialog.errors('保存失败，请联系管理员！！');
            }
        })
    }
</script>
</body>
</html>